package com.vf.admin.client.tx.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.corba.se.spi.ior.ObjectKey;
import com.tencentyun.TLSSigAPIv2;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.tx.dto.im.*;
import com.vf.admin.client.tx.vo.UserShiZhi;
import com.vf.admin.utils.httpqingqiu.HttpPostUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.client.tx.utils.txConfig.*;
import static com.vf.admin.common.Constants.CODE_RETURN_TX;
import static com.vf.admin.utils.RandomUtil.generateUID;
import static com.vf.admin.utils.StringEx.isNull;

/**
 * 腾讯ＩＭ
 */
public class TxIm {

    private static final Logger logger = LoggerFactory.getLogger(TxIm.class);

    /**
     * 单聊消息发送之前回调
     */
    public static final String IMCALLBACKBEFORESENDMSG = "C2C.CallbackBeforeSendMsg";
    /**
     * 新成员入群之后回调
     */
    public static final String IMCALLBACKAFTERNEWMEMBERJOIN = "Group.CallbackAfterNewMemberJoin";
    /**
     * 群成员离开之后回调
     */
    public static final String IMCALLBACKAFTERMEMBEREXIT = "Group.CallbackAfterMemberExit";
    /**
     * 状态变更回调
     */
    public static final String IMSTATECHANGE = "State.StateChange";

    //群成员发送消息后回调
    public static final String AFTERSENDMSG = "Group.CallbackAfterSendMsg";

    //单聊消息撤回后回调
    public static final String CallbackAfterMsgWithDraw = "GC2C.CallbackAfterMsgWithDraw";

    /**
     * 签名生成
     *
     * @return
     */
    public static String ImSignatureGeneration() {
        //腾讯云IM签名创建 所有IM服务端接口都需要签名信息
        TLSSigAPIv2 ssigApi = new TLSSigAPIv2(SDKAPPID, KEY);
        //有效期1小时：60*60秒
        //String sigStr = ssigApi.genSig("123",3600);
        String sigStr = ssigApi.genSig(ADMIN, 3600);
        System.out.println(sigStr);
        return sigStr;
    }


    /**
     * 根据用户ID生成签名
     *
     * @return
     */
    public static String createImSign(String uid) {
        //腾讯云IM签名创建 所有IM服务端接口都需要签名信息
        TLSSigAPIv2 ssigApi = new TLSSigAPIv2(SDKAPPID, KEY);
        //有效期1小时：60*60秒
        return ssigApi.genSig(uid, 3600);
    }


    /**
     * 获取用户在线状态
     *
     * @return
     * @throws Exception
     */
    public static Map<String, Object> imOnline(String userCode) throws Exception {
        Map<String, Object> toMap = new HashMap<>(16);
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/openim/querystate?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        TxImZaiXian zaiXian = new TxImZaiXian();
        zaiXian.setIsNeedDetail(1);
        List<String> userIdS = new ArrayList<String>();
        userIdS.add(userCode);
        zaiXian.setTo_Account(userIdS);
        String jsonConent = JSON.toJSONString(zaiXian);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        Object errorCode = res.get("ErrorCode");
        Object queryResult = res.get("QueryResult");
        JSONArray jsonArr = JSONArray.parseArray(queryResult.toString());
        Map<String, Object> res2 = (Map<String, Object>) jsonArr.get(0);
        toMap.put("code", errorCode);
        toMap.put("State", res2.get("State"));
        return toMap;
    }

    /**
     * 设置用户签名
     *
     * @param shiZhi 提交专用
     * @return
     * @throws Exception
     */
    public static boolean portraitSet(UserShiZhi shiZhi) {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/profile/portrait_set?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        PortraitSet portraitSet = new PortraitSet();
        portraitSet.setFromAccount(shiZhi.getTengxuncode());
        List<Object> list = new ArrayList<Object>();
        //昵称
        Map<String, Object> nicheng = new HashMap<>(16);
        nicheng.put("Tag", "Tag_Profile_IM_SelfSignature");
        nicheng.put("Value", shiZhi.getUserSign());
        list.add(nicheng);
        portraitSet.setProfileItem(list);
        String jsonConent = JSON.toJSONString(portraitSet);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println("resultContent:" + resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        Object errorCode = res.get("ErrorCode");
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置用户昵称
     *
     * @param shiZhi 提交专用
     * @return
     * @throws Exception
     */
    public static boolean portraitNiChengSet(UserShiZhi shiZhi) throws Exception {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/profile/portrait_set?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        PortraitSet portraitSet = new PortraitSet();
        portraitSet.setFromAccount(shiZhi.getTengxuncode());
        List<Object> list = new ArrayList<Object>();
        //昵称
        Map<String, Object> nicheng = new HashMap<>(16);
        nicheng.put("Tag", "Tag_Profile_IM_Nick");
        nicheng.put("Value", shiZhi.getNicheng());
        list.add(nicheng);
        portraitSet.setProfileItem(list);
        String jsonConent = JSON.toJSONString(portraitSet);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println("resultContent:" + resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        Object errorCode = res.get("ErrorCode");
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置用户头像
     *
     * @param shiZhi 提交专用
     * @return
     * @throws Exception
     */
    public static boolean portraitTouXiangSet(UserShiZhi shiZhi) throws Exception {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/profile/portrait_set?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        PortraitSet portraitSet = new PortraitSet();
        portraitSet.setFromAccount(shiZhi.getTengxuncode());
        List<Object> list = new ArrayList<Object>();
        //昵称
        Map<String, Object> nicheng = new HashMap<>(16);
        nicheng.put("Tag", "Tag_Profile_IM_Image");
        nicheng.put("Value", shiZhi.getTouxiang());
        list.add(nicheng);
        portraitSet.setProfileItem(list);
        String jsonConent = JSON.toJSONString(portraitSet);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println("resultContent:" + resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        Object errorCode = res.get("ErrorCode");
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 拉取用户资料
     *
     * @return
     * @throws Exception
     */
    public static String portraitGget(Integer userCode) {
        Map<String, Object> toMap = new HashMap<>(16);
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/profile/portrait_get?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        PortraitGet portraitGet = new PortraitGet();
        List<Object> list0 = new ArrayList<Object>();
        list0.add(userCode + "");
        portraitGet.setTo_Account(list0);
        List<Object> list = new ArrayList<Object>();
        //签名
        list.add("Tag_Profile_IM_SelfSignature");
        portraitGet.setTagList(list);
        String jsonConent = JSON.toJSONString(portraitGet);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        System.out.println(res);
        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        //用户资料列表
        Object UserProfileItem = res.get("UserProfileItem");
        JSONArray jsonArr1 = JSONArray.parseArray(UserProfileItem.toString());
        Map<String, Object> res1 = (Map<String, Object>) jsonArr1.get(0);
        //获取单个用户资料
        Object ProfileItem = res1.get("ProfileItem");
        System.out.println("ProfileItem:" + ProfileItem);
        if (!isNull(ProfileItem)) {

            JSONArray jsonArr2 = JSONArray.parseArray(ProfileItem.toString());
            String a = "";
            for (Object o : jsonArr2) {
                Map<String, Object> res2 = (Map<String, Object>) o;
                a = new String(res2.get("Value").toString());
            }
            if (CODE_RETURN_TX.equals(errorCode.toString())) {
                return a;
            } else {
                return "";
            }
        }else{
            return "";
        }
    }

    /**
     * 添加用户
     *
     * @return
     * @throws Exception
     */
    public static int accountImport(Integer userCode, String nick, String pic) throws Exception {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/im_open_login_svc/account_import?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        AccountImport accountImport = new AccountImport();
        accountImport.setIdentifier(userCode + "");
        accountImport.setFaceUrl(pic);
        accountImport.setNick(nick);
        String jsonConent = JSON.toJSONString(accountImport);
        System.out.println("jsonConent:" + jsonConent);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        Object actionStatus = res.get("ActionStatus");
        Object errorCode = res.get("ErrorCode");
        Object errorInfo = res.get("ErrorInfo");
        System.out.println(resultContent);
        System.out.println(errorInfo);
        return Integer.valueOf(errorCode.toString());
    }

    /**
     * 拉黑一个用户
     * @param fromAccount
     * @param toAccount
     */
    public static void addBlack(Integer fromAccount, Integer toAccount) {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/sns/black_list_add?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        Map<String, Object> params = new HashMap<>();
        params.put("From_Account", String.valueOf(fromAccount));
        List<String> toAccountList = new ArrayList<>();
        toAccountList.add(String.valueOf(toAccount));
        params.put("To_Account", toAccountList);

        String jsonConent = JSON.toJSONString(params);
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        String ActionStatus = res.get("ActionStatus").toString();
        if(!ActionStatus.equalsIgnoreCase("OK")) {
            String ErrorCode = res.get("ErrorCode").toString();
            String ErrorInfo = res.get("ErrorInfo").toString();
            String ErrorDisplay = res.get("ErrorDisplay").toString();
            logger.error("拉黑用户时发生错误 ErrorCode：" + ErrorCode + "ErrorInfo：" + ErrorInfo + "ErrorDisplay：" + ErrorDisplay);
        }
    }

    /**
     * 删除黑名单
     * @param fromAccount
     * @param toAccount
     */
    public static void delBlack(Integer fromAccount, Integer toAccount) {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/sns/black_list_delete?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        Map<String, Object> params = new HashMap<>();
        params.put("From_Account", String.valueOf(fromAccount));
        List<String> toAccountList = new ArrayList<>();
        toAccountList.add(String.valueOf(toAccount));
        params.put("To_Account", toAccountList);

        String jsonConent = JSON.toJSONString(params);
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        String ActionStatus = res.get("ActionStatus").toString();
        if(!ActionStatus.equalsIgnoreCase("OK")) {
            String ErrorCode = res.get("ErrorCode").toString();
            String ErrorInfo = res.get("ErrorInfo").toString();
            String ErrorDisplay = res.get("ErrorDisplay").toString();
            logger.error("删除黑名单时发生错误 ErrorCode：" + ErrorCode + "ErrorInfo：" + ErrorInfo + "ErrorDisplay：" + ErrorDisplay);
        }
    }

    /**
     * 创建群组
     *
     * @return
     * @throws Exception
     */
    public static int createGroup(String userCode, String groupId, String roomName) throws Exception {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/group_open_http_svc/create_group?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        CreateGroup createGroup = new CreateGroup();
        createGroup.setOwner_Account(userCode + "");
        createGroup.setName(roomName);
        createGroup.setType("AVChatRoom");
        createGroup.setGroupId(groupId + "");
        createGroup.setApplyJoinOption("FreeAccess");
        String jsonConent = JSON.toJSONString(createGroup);
        System.out.println("jsonConent:" + jsonConent);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        Object actionStatus = res.get("ActionStatus");
        Object errorCode = res.get("ErrorCode");
        Object errorInfo = res.get("ErrorInfo");
        System.out.println(errorInfo);
        System.out.println(errorCode);
        return Integer.valueOf(errorCode.toString());
    }

    /**
     * 拉取群组资料
     *
     * @return
     * @throws Exception
     */
    public static boolean getGroupInfo(String userCode) throws Exception {
        Map<String, Object> toMap = new HashMap<>(16);
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_info?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        GroupInfo groupInfo = new GroupInfo();
        List<Object> list0 = new ArrayList<Object>();
        list0.add(userCode);
        groupInfo.setGroupIdList(list0);
        String jsonConent = JSON.toJSONString(groupInfo);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;

        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        //群组资料列表
        Object GroupInfo = res.get("GroupInfo");
        JSONArray jsonArr1 = JSONArray.parseArray(GroupInfo.toString());
        Map<String, Object> res1 = (Map<String, Object>) jsonArr1.get(0);
        //获取单个群组资料
        //群组名
        Object Name = res1.get("Name");
        System.out.println(Name);
        //群组id
        Object GroupId = res1.get("GroupId");
        System.out.println(GroupId);

        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 解散群组
     *
     * @return
     * @throws Exception
     */
    public static boolean destroyGroup(String userCode) {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/group_open_http_svc/destroy_group?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        DestroGroup destroGroup = new DestroGroup();
        destroGroup.setGroupId(userCode);
        String jsonConent = JSON.toJSONString(destroGroup);
        System.out.println("jsonConent:" + jsonConent);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        Object actionStatus = res.get("ActionStatus");
        Object errorCode = res.get("ErrorCode");
        Object errorInfo = res.get("ErrorInfo");
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 拉取群组在线人数
     *
     * @return
     * @throws Exception
     */
    public static int getOnlineMemberNum(Object userCode) throws Exception {
        Map<String, Object> toMap = new HashMap<>(16);
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/group_open_http_svc/get_online_member_num?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        CreateGroup createGroup = new CreateGroup();
        createGroup.setGroupId(userCode + "");
        String jsonConent = JSON.toJSONString(createGroup);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;

        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        //在线人数
        Object OnlineMemberNum = res.get("OnlineMemberNum");

        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            if (isNull(OnlineMemberNum)) {
                return 0;
            } else {
                return Integer.valueOf(OnlineMemberNum.toString());
            }
        } else {
            return 0;
        }
    }

    /**
     * 发送群组系统通知
     *
     * @return
     * @throws Exception
     */
    public static boolean sendGroupSystemNotification(String userCode) throws Exception {
        Map<String, Object> toMap = new HashMap<>(16);
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_system_notification?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        SystemNotification systemNotification = new SystemNotification();
        systemNotification.setGroupId(userCode);
        systemNotification.setContent("asjkndkasndj系统通知ksandkasndasda");
        String jsonConent = JSON.toJSONString(systemNotification);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;

        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取群组成员信息
     *
     * @return
     * @throws Exception
     */
    public static String getGroupMemberInfo(Integer userCode) throws Exception {
        Map<String, Object> toMap = new HashMap<>(16);
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/group_open_http_svc/get_group_member_info?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        SystemNotification systemNotification = new SystemNotification();
        systemNotification.setGroupId(userCode + "");
        String jsonConent = JSON.toJSONString(systemNotification);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;

        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object MemberList = res.get("MemberList");
        System.out.println(MemberList);
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return MemberList.toString();
        } else {
            return "";
        }
    }

    /**
     * 拉取运营数据
     *
     * @return
     * @throws Exception
     */
    public static boolean getappinfo() throws Exception {
        Map<String, Object> toMap = new HashMap<>(16);
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/openconfigsvr/getappinfo?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        SystemNotification systemNotification = new SystemNotification();
        String jsonConent = JSON.toJSONString(systemNotification);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;

        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object Result = res.get("Result");
        System.out.println("Result:" + Result);
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 群组中发送普通消息
     * 自定义消息
     *
     * @return
     * @throws Exception
     */
    public static boolean sendGroupMsg(Integer groupId, Object json) throws Exception {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        FaSong faSong = new FaSong();
        faSong.setGroupId(groupId + "");
        faSong.setRandom(Integer.valueOf(generateUID()));
        List<Object> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>(16);
        map.put("MsgType", "TIMTextElem");
        Map<String, Object> map2 = new HashMap<>(16);
        map2.put("Text", json);
        map.put("MsgContent", map2);
        list.add(map);
        faSong.setMsgBody(list);
        String jsonConent = JSON.toJSONString(faSong);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object ErrorInfo = res.get("ErrorInfo");
        System.out.println(ErrorInfo);
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 拉取群历史消息
     *
     * @return
     * @throws Exception
     */
    public static boolean groupMsgGetSimple(String userCode) throws Exception {
        Map<String, Object> toMap = new HashMap<>(16);
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/group_open_http_svc/group_msg_get_simple?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        Laqu lq = new Laqu();
        lq.setGroupId(userCode);
        lq.setReqMsgNumber(10);

        String jsonConent = JSON.toJSONString(lq);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;

        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object ErrorInfo = res.get("ErrorInfo");
        System.out.println(ErrorInfo);
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取用户所加入的群组
     *
     * @return
     * @throws Exception
     */
    public static boolean getJoinedGroupList(String userCode) throws Exception {
        Map<String, Object> toMap = new HashMap<>(16);
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/group_open_http_svc/get_joined_group_list?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        GetQun lq = new GetQun();
        lq.setMember_Account(userCode);
        String jsonConent = JSON.toJSONString(lq);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;

        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object ErrorInfo = res.get("ErrorInfo");
        System.out.println(ErrorInfo);
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 增加群成员
     *
     * @return
     * @throws Exception
     */
    public static boolean addGroupMember(String userCode, String qunCode) throws Exception {
        Map<String, Object> toMap = new HashMap<>(16);
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/group_open_http_svc/add_group_member?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        JaQun jq = new JaQun();
        jq.setGroupId(qunCode);
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>(16);
        map.put("Member_Account", userCode);
        list.add(map);
        jq.setMemberList(list);
        String jsonConent = JSON.toJSONString(jq);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;

        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object ErrorInfo = res.get("ErrorInfo");
        System.out.println(ErrorInfo);
        System.out.println(errorCode);
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 发送礼物消息
     * @param toAccount
     * @param fromAccount
     * @param img
     * @param giftName
     * @param syncOtherMachine
     * @return
     */
    public static boolean sendUserGiftMsg(Integer toAccount, Integer fromAccount, Integer giftsId, String giftName,
                                          String giftsPrice, String img, String svgaUrl, Integer syncOtherMachine) {
        List<String> collBackControlList = new ArrayList();
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        UserFaSong faSong = new UserFaSong();
        faSong.setSyncOtherMachine(syncOtherMachine);
        faSong.setToAccount(toAccount + "");
        faSong.setFromAccount(fromAccount + "");
        faSong.setMsgRandom(Integer.valueOf(generateUID()));
        collBackControlList.add("ForbidBeforeSendMsgCallback");
        collBackControlList.add("ForbidAfterSendMsgCallback");
        faSong.setForbidCallbackControl(collBackControlList);
        List<Object> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>(16);
        map.put("MsgType", "TIMCustomElem");

        Map<String, Object> map2 = new HashMap<>(16);
        map2.put("text", "");
        map2.put("img", img);
        map2.put("uuid", "");
        map2.put("giftsId", giftsId);
        map2.put("giftsName", giftName);
        map2.put("giftsPrice", giftsPrice);
        map2.put("svgaUrl", svgaUrl);
        map2.put("giftsNum", 1);
        map2.put("type", "@@@@@***");
        String jsonConent2 = JSON.toJSONString(map2);

        Map<String, Object> map3 = new HashMap<>(16);
        map3.put("Data", jsonConent2);

        map.put("MsgContent", map3);



        list.add(map);
        faSong.setMsgBody(list);
        String jsonConent = JSON.toJSONString(faSong);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object ErrorInfo = res.get("ErrorInfo");
        System.out.println(ErrorInfo);
        System.out.println(errorCode);
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 发送爱神牵线通知
     * @param toAccount
     * @param fromAccount
     * @param text
     * @return
     */
    public static boolean sendUserMsgqbt(Integer toAccount, Integer fromAccount, String text) {
        List<String> collBackControlList = new ArrayList();
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        UserFaSong faSong = new UserFaSong();
        faSong.setSyncOtherMachine(2);
        faSong.setToAccount(toAccount + "");
        faSong.setFromAccount(fromAccount + "");
        faSong.setMsgRandom(Integer.valueOf(generateUID()));
        collBackControlList.add("ForbidBeforeSendMsgCallback");
        collBackControlList.add("ForbidAfterSendMsgCallback");
        faSong.setForbidCallbackControl(collBackControlList);

        List<Object> list = new ArrayList<>();
        Map<String, Object> map2 = new HashMap<>(16);
        map2.put("text", text);
        map2.put("uuid", "qbt");
        map2.put("type", "@@@@@**");
        String jsonConent2 = JSON.toJSONString(map2);

        Map<String, Object> map3 = new HashMap<>(16);
        map3.put("Data", jsonConent2);

        Map<String, Object> map = new HashMap<>(16);
        map.put("MsgType", "TIMCustomElem");
        map.put("MsgContent", map3);

        list.add(map);
        faSong.setMsgBody(list);
        String jsonConent = JSON.toJSONString(faSong);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object ErrorInfo = res.get("ErrorInfo");
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 发送爱神牵线通知
     * 居中通知
     * @param toAccount
     * @param fromAccount
     * @param text
     * @return
     */
    public static boolean sendUserMsgqbtAlert(Integer toAccount, Integer fromAccount, String text) {
        List<String> collBackControlList = new ArrayList();
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        UserFaSong faSong = new UserFaSong();
        faSong.setSyncOtherMachine(2);
        faSong.setToAccount(toAccount + "");
        faSong.setFromAccount(fromAccount + "");
        faSong.setMsgRandom(Integer.valueOf(generateUID()));
        collBackControlList.add("ForbidBeforeSendMsgCallback");
        collBackControlList.add("ForbidAfterSendMsgCallback");
        faSong.setForbidCallbackControl(collBackControlList);
        List<Object> list = new ArrayList<>();

        Map<String, Object> map2 = new HashMap<>(16);
        map2.put("text", text);
        map2.put("uuid", "qbt");
        map2.put("type", "@@@@@*");
        String jsonConent2 = JSON.toJSONString(map2);

        Map<String, Object> map3 = new HashMap<>(16);
        map3.put("Data", jsonConent2);

        Map<String, Object> map = new HashMap<>(16);
        map.put("MsgType", "TIMCustomElem");
        map.put("MsgContent", map3);

        list.add(map);
        faSong.setMsgBody(list);
        String jsonConent = JSON.toJSONString(faSong);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object ErrorInfo = res.get("ErrorInfo");
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 发送点赞/评论打招呼消息
     * @param toAccount
     * @param fromAccount
     * @param dynamicId
     * @param dynamicImage
     * @return
     */
    public static boolean sendDynamicHello(Integer toAccount, Integer fromAccount, String text, Integer dynamicId, String dynamicImage) {
        List<String> collBackControlList = new ArrayList();
        String sigStr = ImSignatureGeneration();
        String url = "https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        UserFaSong faSong = new UserFaSong();
        faSong.setSyncOtherMachine(1);
        faSong.setToAccount(toAccount + "");
        faSong.setFromAccount(fromAccount + "");
        faSong.setMsgRandom(Integer.valueOf(generateUID()));
        collBackControlList.add("ForbidBeforeSendMsgCallback");
        collBackControlList.add("ForbidAfterSendMsgCallback");
        faSong.setForbidCallbackControl(collBackControlList);

        List<Object> list = new ArrayList<>();
        Map<String, Object> map2 = new HashMap<>(16);
        map2.put("text", text);
        map2.put("uuid", "qbt");
        map2.put("type", "@@@@@dynamic");
        map2.put("image", dynamicImage);
        map2.put("dynamicId", dynamicId);
        String jsonConent2 = JSON.toJSONString(map2);

        Map<String, Object> map3 = new HashMap<>(16);
        map3.put("Data", jsonConent2);

        Map<String, Object> map = new HashMap<>(16);
        map.put("MsgType", "TIMCustomElem");
        map.put("MsgContent", map3);

        list.add(map);
        faSong.setMsgBody(list);
        String jsonConent = JSON.toJSONString(faSong);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 发送系统消息
     * @param toAccount
     * @param text
     * @return
     */
    public static boolean sendSystemAlert(Integer toAccount, String text) {
        List<String> collBackControlList = new ArrayList();
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        UserFaSong faSong = new UserFaSong();
        faSong.setSyncOtherMachine(2);
        faSong.setToAccount(toAccount + "");
        faSong.setMsgRandom(Integer.valueOf(generateUID()));
        collBackControlList.add("ForbidBeforeSendMsgCallback");
        collBackControlList.add("ForbidAfterSendMsgCallback");
        faSong.setForbidCallbackControl(collBackControlList);
        List<Object> list = new ArrayList<>();

        Map<String, Object> map2 = new HashMap<>(16);
        map2.put("Text", text);

        Map<String, Object> map = new HashMap<>(16);
        map.put("MsgType", "TIMTextElem");
        map.put("MsgContent", map2);

        list.add(map);
        faSong.setMsgBody(list);
        String jsonConent = JSON.toJSONString(faSong);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 查询某人A和某人B之间的消息
     * 自定义消息
     *
     * @return
     * @throws Exception
     */
    public static boolean adminGetroammsg(Integer toAccount,Integer fromAccount) throws Exception {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/openim/admin_getroammsg?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        Getroammsg getroammsg = new Getroammsg();
        getroammsg.setToAccount(toAccount + "");
        getroammsg.setFromAccount(fromAccount + "");
        getroammsg.setMaxCnt(100);
        getroammsg.setMinTime(1637055032);
        getroammsg.setMaxTime(1637062232);
        String jsonConent = JSON.toJSONString(getroammsg);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object ErrorInfo = res.get("ErrorInfo");
        System.out.println(ErrorInfo);
        System.out.println(errorCode);
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        try {
            //System.out.println(ImSignatureGeneration());
            /*
             * 导入用户
             * return:true成功  false失败
             */

/*
            System.out.println(accountImport(648968579, "哈哈", "http://user.png"));
*/
        /*
             * 查询用户在线状态
             */
            //System.out.println(imOnline("659356883"));
            /*
             * 更新用户资料
             * return:true成功  false失败
             */
           /* userSignVo vo=new userSignVo();
            vo.setCharmNum(1000);
            vo.setHeadHalo("http://光晕.png");
            vo.setHeadWear("http://头饰.png");
            net.sf.json.JSONObject obj = net.sf.json.JSONObject.fromObject(vo);
*//*
            String json=obj.toString().replace("\"","\"");
*//*
            UserShiZhi shiZhi=new UserShiZhi();
            shiZhi.setUserSign(obj.toString());
            shiZhi.setTengxuncode("123456792");
            System.out.println(portraitSet(shiZhi));
            /**
             * 获取用户资料
             */
            //String re = portraitGget(123456791);
            //JSONObject jsonObject = JSON.parseObject(re);
/*
            String charmNum = jsonObject.getString("charmNum");
*/
            //System.out.println("jsonObject:" + jsonObject);

            //创建群组【不需要】
            //System.out.println(createGroup("administrator","100001","官方群"));

            //获取群组信息
            //System.out.println(getGroupInfo("100001"));
            //解散群组
            //System.out.println(destroyGroup("12345678"));
            //拉取群组在线人数
            //System.out.println("拉取群组在线人数");
            //System.out.println(getOnlineMemberNum(123456792));
            //发送系统通知
            //System.out.println(sendGroupSystemNotification("123456792"));
            //获取群组成员信息
            //System.out.println("获取群组成员信息");
            //System.out.println(getGroupMemberInfo(123456792+""));
            //发送消息
            /*Result result=new Result();
            RoomGiftVo vo=new RoomGiftVo();
            vo.setCharmNum(1);
            vo.setShouliUser(1);
            vo.setSongliUser(1);
            result.setCode(20);
            result.setData(vo);
            net.sf.json.JSONObject obj = net.sf.json.JSONObject.fromObject(result);
            String json=obj.toString().replace("\"","\"");
            System.out.println(sendGroupMsg(123456792,json));*/
            //拉取消息
            //System.out.println(groupMsgGetSimple("123456792"));
            //获取用户加入的群组
            //System.out.println(getJoinedGroupList("123456789"));
            //获取app的运营数据
            //System.out.println(getappinfo());
            //加群
            //System.out.println(addGroupMember("123456792","miaodong"));
/*
            System.out.println(sendUserMsg(552357573,742374681,"你好","http://www.lieqiba.com/uploads/allimg/170719/11420035Q-0.jpg","5x41z6c1a5616w"));
*/
            //查询消息
            //System.out.println(adminGetroammsg(742374681,268772414));
            //sendDynamicHello(814769246, 587552132, "嗨~我刚刚评论了你的动态~", 1, "http://yunliaos.oss-cn-beijing.aliyuncs.com/image/7b0812c94d954d9aa7fc879a477c01a7.jpg");
            delBlack(189684299, 569638429);
        } catch (Exception e) {

        }
    }


    public static Map<String, Object> adminGetroammsg123(Integer toAccount,Integer fromAccount,Integer minTime,Integer maxTime) throws Exception {
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/openim/admin_getroammsg?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        Getroammsg getroammsg = new Getroammsg();
        getroammsg.setToAccount(toAccount + "");
        getroammsg.setFromAccount(fromAccount + "");
        getroammsg.setMaxCnt(100);
        getroammsg.setMinTime(minTime);
        getroammsg.setMaxTime(maxTime);
        String jsonConent = JSON.toJSONString(getroammsg);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        return res;
        //状态码  0正常
        /*Object errorCode = res.get("ErrorCode");
        Object ErrorInfo = res.get("ErrorInfo");
        System.out.println(ErrorInfo);
        System.out.println(errorCode);
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }*/
    }


    /**
     *
     * @param toAccount 接受人腾讯code
     * @param fromAccount 发送人腾讯code
     * @param text 发送文本消息内容
     * @return
     * @throws Exception
     */
    public static boolean sendUserMsgNew(Integer toAccount,Integer fromAccount,String text,String msgType) {
        List<String> collBackControlList = new ArrayList();
        String sigStr = ImSignatureGeneration();
        //用户腾讯IM用户在线状态
        String url = "https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=" + SDKAPPID + "&identifier=" + ADMIN + "&usersig=" + sigStr + "&random=99999999&contenttype=json";
        //封装参数
        UserFaSong faSong = new UserFaSong();
        faSong.setSyncOtherMachine(1);
        faSong.setToAccount(toAccount + "");
        faSong.setFromAccount(fromAccount + "");
        faSong.setMsgRandom(Integer.valueOf(generateUID()));
        collBackControlList.add("ForbidBeforeSendMsgCallback");
        collBackControlList.add("ForbidAfterSendMsgCallback");
        faSong.setForbidCallbackControl(collBackControlList);
        List<Object> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>(16);
        map.put("MsgType", msgType);

        Map<String, Object> map2 = new HashMap<>(16);
        map2.put("Text", text);
        Map<String, Object> map3 = new HashMap<>(16);
        String jsonConent2 = JSON.toJSONString(map2);

        map3.put("Data", jsonConent2);

        map3.put("Text", text);
        map.put("MsgContent", map3);


        list.add(map);
        faSong.setMsgBody(list);
        String jsonConent = JSON.toJSONString(faSong);
        //发送请求
        String resultContent = HttpPostUtils.getInstance().sendPostBody(url, "", jsonConent);
//        System.out.println(resultContent);
        JSONObject jsonObject = JSONObject.parseObject(resultContent);
        Map<String, Object> res = (Map<String, Object>) jsonObject;
        //状态码  0正常
        Object errorCode = res.get("ErrorCode");
        Object ErrorInfo = res.get("ErrorInfo");
        if (CODE_RETURN_TX.equals(errorCode.toString())) {
            return true;
        } else {
            return false;
        }
    }

}
